home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / posix / execvp.c < prev    next >
C/C++ Source or Header  |  1992-04-29  |  3KB  |  113 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <unistd.h>
  21. #include <stdarg.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <limits.h>
  26. #include <sys/types.h>
  27.  
  28. #ifndef    HAVE_GNU_LD
  29. #define    __environ    environ
  30. #endif
  31.  
  32. /* Execute FILE, searching in the `PATH' environment variable if it contains
  33.    no slashes, with arguments ARGV and environment from `environ'.  */
  34. int
  35. DEFUN(execvp, (file, argv), CONST char *file AND char *CONST argv[])
  36. {
  37.   if (strchr (file, '/') == NULL)
  38.     {
  39.       char *path, *p;
  40.       struct stat st;
  41.       size_t len;
  42.       uid_t uid;
  43.       gid_t gid;
  44.       int ngroups;
  45.       gid_t groups[NGROUPS_MAX];
  46.       char *name;
  47.  
  48.       path = getenv ("PATH");
  49.       if (path == NULL)
  50.     {
  51.       /* There is no `PATH' in the environment.
  52.          The default search path is the current directory
  53.          followed by the path `confstr' returns for `_CS_PATH'.  */
  54.       len = confstr (_CS_PATH, (char *) NULL, 0);
  55.       path = (char *) __alloca (1 + len);
  56.       path[0] = ':';
  57.       (void) confstr (_CS_PATH, path + 1, len);
  58.     }
  59.  
  60.       len = strlen (file) + 1;
  61.       name = __alloca (strlen (path) + len);
  62.       uid = geteuid ();
  63.       gid = getegid ();
  64.       ngroups = getgroups (sizeof (groups) / sizeof (groups[0]), groups);
  65.       p = path;
  66.       do
  67.     {
  68.       path = p;
  69.       p = strchr (path, ':');
  70.       if (p == NULL)
  71.         p = strchr (path, '\0');
  72.  
  73.       if (p == path)
  74.         /* Two adjacent colons, or a colon at the beginning or the end
  75.            of `PATH' means to search the current directory.  */
  76.         (void) memcpy (name, file, len);
  77.       else
  78.         {
  79.           /* Construct the pathname to try.  */
  80.           (void) memcpy (name, path, p - path);
  81.           name[p - path] = '/';
  82.           (void) memcpy (&name[(p - path) + 1], file, len);
  83.         }
  84.       if (stat (name, &st) == 0 && S_ISREG (st.st_mode))
  85.         {
  86.           int bit = S_IXOTH;
  87.           if (st.st_uid == uid)
  88.         bit = S_IXUSR;
  89.           else if (st.st_gid == gid)
  90.         bit = S_IXGRP;
  91.           else
  92.         {
  93.           register int i;
  94.           for (i = 0; i < ngroups; ++i)
  95.             if (st.st_gid == groups[i])
  96.               {
  97.             bit = S_IXGRP;
  98.             break;
  99.               }
  100.         }
  101.           if (st.st_mode & bit)
  102.         {
  103.           file = name;
  104.           break;
  105.         }
  106.         }
  107.     }
  108.       while (*p++ != '\0');
  109.     }
  110.  
  111.   return __execve (file, argv, __environ);
  112. }
  113.